package sol.problems;

import java.util.Arrays;

/**
 * Created by lkk on 2016/8/18.
 */
public class VectorAdd {

    public static int[] add(String vectorCollection) {
        int x = 0;//记录当前向量的x
        int y = 0;//记录当前向量的y
        boolean isX = true;//标记正在处理X还是Y
        int[] res = {0, 0};//结果向量

        for (int i = 0; i < vectorCollection.length(); ++i) {
            char c = vectorCollection.charAt(i);
            //如果当前字符是数字
            if (c >= '0' && c <= '9') {
                int n = c - '0';
                if (isX)
                    x = 10 * x + n;
                else
                    y = 10 * y + n;
            }
            //如果当前字符是右括号，当前向量解析完毕
            else if (c == ')') {
                res[0] += x;
                res[1] += y;

                x = 0;
                y = 0;
            }
            //如果是逗号
            else if (c == ',') {
                isX = !isX;
            }
            //其他情况则跳过处理
            else
                continue;

        }
        return res;
    }

    public static void main(String[] args) {
        String vectors = "{(42,8),(1,1)}";
        int[] res = add(vectors);
        System.out.println(Arrays.toString(res));
    }
}
